package org.dentalcard.service.impl;

import org.dentalcard.entity.DecUser;
import org.dentalcard.service.PersistenceManager;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 19.12.11
 * @time: 22:35
 */
public class DecUserDaoImpl {
    
    public DecUser getUser(String username, String password){
        Session session = PersistenceManager.getSession();
        try {
            Transaction transaction = session.beginTransaction();
            try {
                Criteria criteria = session.createCriteria(DecUser.class);
                criteria.add(Restrictions.eq("name", username));
                criteria.add(Restrictions.eq("password", password));
                return (DecUser) criteria.uniqueResult();
            } finally {
                transaction.rollback();
            }
        } finally {
            session.close();
        }
    }

    public DecUser getUser(Integer id){
        Session session = PersistenceManager.getSession();
        try {
            return (DecUser) session.get(DecUser.class, id);
        } finally {
            session.close();
        }
    }

    public List<DecUser> getUsers(){
        Session session = PersistenceManager.getSession();
        try {
            Transaction transaction = session.beginTransaction();
            try {
                Criteria criteria = session.createCriteria(DecUser.class);
                return (List<DecUser>) criteria.list();
            } finally {
                transaction.commit();
            }
        } finally {
            session.close();
        }
    }

}
